﻿
Imports System
Imports System.Data
Imports System.Text

Imports Moca.Db

Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports Moca.Db.Helper


Namespace Db.Helper


	'''<summary>
	'''SqlCeDbAccessHelperTest のテスト クラスです。すべての
	'''SqlCeDbAccessHelperTest 単体テストをここに含めます
	'''</summary>
	<TestClass()> _
	Public Class SqlCeDbAccessHelperTest
		Inherits Moca.Test.AbstractUnitTest

		Private testContextInstance As TestContext

		Protected dbaTest As IDaoSqlCeTest

		'''<summary>
		'''現在のテストの実行についての情報および機能を
		'''提供するテスト コンテキストを取得または設定します。
		'''</summary>
		Public Property TestContext() As TestContext
			Get
				Return testContextInstance
			End Get
			Set(value As TestContext)
				testContextInstance = value
			End Set
		End Property

#Region "追加のテスト属性"
		'
		'テストを作成するときに、次の追加属性を使用することができます:
		'
		'クラスの最初のテストを実行する前にコードを実行するには、ClassInitialize を使用
		<ClassInitialize()> _
		Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
			ClassInitialize(testContext)
		End Sub

		'クラスのすべてのテストを実行した後にコードを実行するには、ClassCleanup を使用
		<ClassCleanup()> _
		Public Shared Sub MyClassCleanup()
			ClassCleanup()
		End Sub

        '各テストを実行する前にコードを実行するには、TestInitialize を使用
        <TestInitialize()> _
        Public Sub MyTestInitialize()
            Me.inject()
        End Sub

        '各テストを実行した後にコードを実行するには、TestCleanup を使用
        <TestCleanup()> _
        Public Sub MyTestCleanup()
        End Sub

#End Region

		'''<summary>
		'''Dao Select のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub DaoSelectTest()
			Dim lst As IList(Of tbDemoEntity)
			lst = dbaTest.Select()
			Assert.AreEqual(lst.Count, 1)
			Assert.AreEqual(lst(0).Name, "hoge2")
		End Sub

		'''<summary>
		'''CDbParameterName のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub CDbParameterNameTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim name As String = "hoge"
			Dim expected As String = "@hoge"
			Dim actual As String
			actual = target.CDbParameterName(name)
			Assert.AreEqual(expected, actual)
		End Sub

		'''<summary>
		'''ConnectionTest のテスト
		'''</summary>
        <TestMethod()> _
        Public Sub ConnectionTest()
            Dim target As SqlCeDbAccessHelper = dbaTest.GetHelper
            Dim actual As DataTable
            actual = target.ConnectionTest
        End Sub

		'''<summary>
		'''ErrorCount のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ErrorCountTest()
			Try
				dbaTest.Ins(2)
			Catch ex As Exception
				Dim target As IDbAccessHelper = dbaTest.GetHelper
				Dim expected As Integer = 1
				Dim actual As Integer
				actual = target.ErrorCount(ex.GetBaseException)
				Debug.Print(ex.GetBaseException.Message)
				Assert.AreEqual(expected, actual)
			End Try
		End Sub

		'''<summary>
		'''ErrorNumbers のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub ErrorNumbersTest()
			Try
				dbaTest.Ins(2)
			Catch ex As Exception
				Dim target As IDbAccessHelper = dbaTest.GetHelper
				Dim expected() As String = {"25016"}
				Dim actual() As String
				actual = target.ErrorNumbers(ex.GetBaseException)
				Debug.Print(ex.GetBaseException.Message)
				Assert.AreEqual(expected.Count, actual.Count)
				Assert.AreEqual(expected(0), actual(0))
			End Try
		End Sub

		'''<summary>
		'''GetSchemaColumns のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaColumnsTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim table As DbInfoTable = New DbInfoTable(Nothing, Nothing, "tbDemoEntities", "TABLE")
			Dim actual As DbInfoColumnCollection

			actual = target.GetSchemaColumns(table)
			Assert.AreEqual(4, actual.Count)
			Assert.AreEqual(5, actual("ID").MaxLength)
			Assert.AreEqual(0, actual("ID").Precision)
			Assert.AreEqual(0, actual("ID").Scale)
			Assert.AreEqual(True, actual("ID").UniCode)
			Assert.AreEqual(10, actual("Code").MaxLength)
			Assert.AreEqual(10, actual("Code").Precision)
			Assert.AreEqual(0, actual("Code").Scale)
			Assert.AreEqual(False, actual("Code").UniCode)
			Assert.AreEqual(256, actual("Name").MaxLength)
			Assert.AreEqual(0, actual("Name").Precision)
			Assert.AreEqual(0, actual("Name").Scale)
			Assert.AreEqual(True, actual("Name").UniCode)
			Assert.AreEqual(4000, actual("Note").MaxLength)
			Assert.AreEqual(0, actual("Note").Precision)
			Assert.AreEqual(0, actual("Note").Scale)
			Assert.AreEqual(True, actual("Note").UniCode)
		End Sub

		'''<summary>
		'''GetSchemaFunctions のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaFunctionsTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Try
				target.GetSchemaFunctions()
			Catch ex As Moca.Db.DbAccessException
				Assert.AreEqual(GetType(NotSupportedException), ex.BaseException.GetType)
			End Try
		End Sub

		'''<summary>
		'''GetSchemaProcedures のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaProceduresTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Try
				target.GetSchemaProcedures()
			Catch ex As Moca.Db.DbAccessException
				Assert.AreEqual(GetType(NotSupportedException), ex.BaseException.GetType)
			End Try
		End Sub

		'''<summary>
		'''GetSchemaTable のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaTableTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim tablename As String = "tbDemoEntities"
			Dim actual As DbInfoTable
			actual = target.GetSchemaTable(tablename)
			Assert.AreEqual(Nothing, actual.Catalog)
			Assert.AreEqual(Nothing, actual.Schema)
			Assert.AreEqual(tablename, actual.Name)
			Assert.AreEqual("TABLE", actual.Typ)
		End Sub

		'''<summary>
		'''GetSchemaTables のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub GetSchemaTablesTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim tablename1 As String = "..TABLE.tbDemoEntities"
			Dim obj As Object = Nothing
			Dim actual As DbInfoTableCollection
			actual = target.GetSchemaTables
			Assert.AreEqual(1, actual.Count)
			Assert.AreEqual(True, actual.Keys.Contains(tablename1))
		End Sub

		'''<summary>
		'''HasSqlNativeError のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub HasSqlNativeErrorTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim expected As Boolean = True
			Dim actual As Boolean
			Try
				dbaTest.Ins(2)
			Catch ex As Exception
				actual = target.HasSqlNativeError(ex.GetBaseException, SqlCeErrorNumbers.DuplicationPKey)
				Debug.Print(ex.GetBaseException.Message)
			End Try
			Assert.AreEqual(expected, actual)
		End Sub

		'''<summary>
		'''HasSqlNativeErrorDuplicationPKey のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub HasSqlNativeErrorDuplicationPKeyTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim expected As Boolean = True
			Dim actual As Boolean
			Try
				dbaTest.Ins(2)
			Catch ex As Exception
				actual = target.HasSqlNativeErrorDuplicationPKey(ex.GetBaseException)
				Debug.Print(ex.GetBaseException.Message)
			End Try
			Assert.AreEqual(expected, actual)
		End Sub

		'''<summary>
		'''HasSqlNativeErrorTimtout のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub HasSqlNativeErrorTimtoutTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim expected As Boolean = True
			Dim actual As Boolean
			Try
				dbaTest.TimeoutTest(10)
			Catch ex As Exception
				actual = target.HasSqlNativeErrorTimtout(ex.GetBaseException)
				Debug.Print(ex.GetBaseException.Message)
			End Try
			Assert.AreEqual(expected, actual)
		End Sub

		'''<summary>
		'''RefreshProcedureParameters のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub RefreshProcedureParametersTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Try
				target.RefreshProcedureParameters(Nothing)
			Catch ex As Moca.Db.DbAccessException
				Assert.AreEqual(GetType(NotSupportedException), ex.BaseException.GetType)
			End Try
		End Sub

		'''<summary>
		'''PlaceholderMark のテスト
		'''</summary>
		<TestMethod()> _
		Public Sub PlaceholderMarkTest()
			Dim target As IDbAccessHelper = dbaTest.GetHelper
			Dim actual As String
			actual = target.PlaceholderMark
			Assert.AreEqual("@", actual)
		End Sub
	End Class

End Namespace
